package com.beta.restapi.controller;

import com.beta.common.exception.ControllerException;
import com.beta.common.web.ContentType;
import com.beta.restapi.controller.upload.IImageHandler;
import com.beta.restapi.internal.Consts;
import com.beta.service.ServiceException;
import io.swagger.annotations.Api;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
 * Created by lichao on 2016/12/12.
 */
@Api(tags = "images")
@RestController
@RequestMapping(Consts.API_PATH_PREFIX + Consts.Resource.IMAGES)
public class ImagesController extends BaseController {

    @Autowired
    private ApplicationContext applicationContext;

    @PostMapping(value = "", produces = {ContentType.JSON})
    public Object uploadImage(HttpServletRequest request,
                              @RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                              @RequestParam("picture") MultipartFile multipartFile,
                              @RequestParam("picture_type") String pictureType) {
        if (multipartFile.isEmpty()) {
            throw new ServiceException("E10011");
        }
        String imageUrl = saveFile(request, pictureType, multipartFile);
        return Collections.singletonMap("url", imageUrl);
    }

    @DeleteMapping(value = "", produces = {ContentType.JSON})
    public Object deleteImage(HttpServletRequest request,
                              @RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                              @RequestParam("picture_type") String pictureType) {
        Collection<IImageHandler> imageHandlers = getImageHandlers();
        if (CollectionUtils.isEmpty(imageHandlers)) {
            return error(HttpStatus.UNPROCESSABLE_ENTITY.value(), "E90001");
        }
        for (IImageHandler imageHandler : imageHandlers) {
            if (imageHandler.supportType(pictureType)) {
                imageHandler.deleteImage(request, getCurrentUserId(request));
                break;
            }
        }
        return ok(HttpStatus.NO_CONTENT.value(), null);
    }

    private String saveFile(HttpServletRequest request, String pictureType, MultipartFile multipartFile) {
        for (IImageHandler imageHandler : getImageHandlers()) {
            if (imageHandler.supportType(pictureType)) {
                return imageHandler.saveImage(multipartFile, request, getCurrentUserId(request));
            }
        }
        throw new ControllerException("E90001");
    }

    private Collection<IImageHandler> getImageHandlers() {
        Map<String, IImageHandler> imageHandlers = applicationContext.getBeansOfType(IImageHandler.class);
        if (MapUtils.isEmpty(imageHandlers)) {
            return Collections.emptyList();
        }
        return imageHandlers.values();
    }

}
